#include <algorithm>
#include <cassert>
#include <vector>

using namespace std;

class Solution {
public:
    bool canPlaceFlowers(vector<int>& flowerbed, int n) {
        int count = 0;
        int bedNum = flowerbed.size();
        for (int i = 0; i < bedNum; ++i) {
            if (flowerbed[i] == 0 && canPlaceFlower(i, flowerbed, bedNum)) {
                flowerbed[i] = 1;
                ++count;
            }
        }

        return count >= n;
    }

private:
    bool canPlaceFlower(int pos, const vector<int>& flowerbed, int bedNum) {
        int left = (pos == 0) ? 0 : flowerbed[pos - 1];
        int right = (pos == bedNum - 1) ? 0 : flowerbed[pos + 1];

        return (left == 0 && right == 0);
    }
};

void test() {
    Solution s;

    int n1 = 1;
    int n2 = 2;
    int n3 = 1;

    vector<int> flowerbed1{1, 0, 0, 0, 1};
    vector<int> flowerbed2{1, 0, 0, 0, 1};
    vector<int> flowerbed3{0, 0, 1, 0, 1};

    assert(s.canPlaceFlowers(flowerbed1, n1) == true);
    assert(s.canPlaceFlowers(flowerbed2, n2) == false);
    assert(s.canPlaceFlowers(flowerbed3, n3) == true);
}

int main() {
    test();
    return 0;
}